//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2011, Steven Oda
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the copyright holder nor the names of any
//       contributors may be used to endorse or promote products derived from
//       this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//////////////////////////////////////////////////////////////////////////////

#include "LevelEditorManager.h"
#include <string>

using namespace std;

LevelEditorManager* LevelEditorManager::s_LevelEditorManager = NULL;

LevelEditorManager::LevelEditorManager(){
    //switchboard subscriptions for the class
    theSwitchboard.SubscribeTo(this, "MouseDown");

    //camera control keys
    theSwitchboard.SubscribeTo(this, "UP");
    theSwitchboard.SubscribeTo(this, "LEFT");
    theSwitchboard.SubscribeTo(this, "DOWN");
    theSwitchboard.SubscribeTo(this, "RIGHT");

    //subscribe to number keys
    theSwitchboard.SubscribeTo(this, "KEY1");
    theSwitchboard.SubscribeTo(this, "KEY2");
    theSwitchboard.SubscribeTo(this, "KEY3");
    theSwitchboard.SubscribeTo(this, "KEY4");
    theSwitchboard.SubscribeTo(this, "KEY5");
    theSwitchboard.SubscribeTo(this, "KEY6");
    theSwitchboard.SubscribeTo(this, "KEY7");
    theSwitchboard.SubscribeTo(this, "KEY8");
    theSwitchboard.SubscribeTo(this, "KEY9");
    theSwitchboard.SubscribeTo(this, "KEY0");
    }

LevelEditorManager& LevelEditorManager::GetInstance(){
    //gets the instance of the LevelEditorManager to be used
    //in theWorld.SetGameManager function in Main.cpp
    if(s_LevelEditorManager == NULL){
        s_LevelEditorManager = new LevelEditorManager;
    }
    return *s_LevelEditorManager;
}

void LevelEditorManager::ReceiveMessage(Message* message){
    string m = message->GetMessageName();

    //holds camser position for changing the camera
    Vector2 cam = theCamera.GetPosition();

    //gets the MouseDown message and calls the function editTile that uses it
    if( m == "MouseDown" ){
        TypedMessage<Vec2i> *m = (TypedMessage<Vec2i>*)message;
		Vec2i screenCoordinates = m->GetValue();
		Vector2 worldCoordinates = MathUtil::ScreenToWorld(screenCoordinates);
		editLevel.editTile(worldCoordinates);
    }
    //change the camera position
    else if(m == "UP")    {theCamera.SetPosition(cam.X, cam.Y+1);}
    else if(m == "LEFT")  {theCamera.SetPosition(cam.X-1, cam.Y);}
    else if(m == "DOWN")  {theCamera.SetPosition(cam.X, cam.Y-1);}
    else if(m == "RIGHT") {theCamera.SetPosition(cam.X+1, cam.Y);}

    //change the color using the number keys
    else if( m == "KEY1"){editLevel.setTempTileType(1);}
    else if( m == "KEY2"){editLevel.setTempTileType(2);}
    else if( m == "KEY3"){editLevel.setTempTileType(3);}
    else if( m == "KEY4"){editLevel.setTempTileType(4);}
    else if( m == "KEY5"){editLevel.setTempTileType(5);}
    else if( m == "KEY6"){editLevel.setTempTileType(6);}
    else if( m == "KEY7"){editLevel.setTempTileType(7);}
    else if( m == "KEY8"){editLevel.setTempTileType(8);}
    else if( m == "KEY9"){editLevel.setTempTileType(9);}
    else if( m == "KEY0"){editLevel.setTempTileType(0);}
}

 void LevelEditorManager::MouseDownEvent(Vec2i screenCoordinates, MouseButtonInput button){
    //broadcast when the mouse button is down
	TypedMessage<Vec2i> *m = new TypedMessage<Vec2i>("MouseDown", screenCoordinates);
	theSwitchboard.Broadcast(m);
}

void LevelEditorManager::load(string file){
    //open the ActorDef File
    editLevel.loadArchetype("tile_system");
    //open the file to edit
    editLevel.load(file);
}
void LevelEditorManager::save(string file){
    //save the edited level to the file
    editLevel.save(file);
}

